from rest_framework.viewsets import GenericViewSet

from apps.models import User, Address
from apps.serializers.address import AddressSerializer
from apps.utils.common_mixin import APICreateModelMixin, APIListModelMixin,APIRetrieveModelMixin
from apps.utils.common_response import APIResponse
from rest_framework.mixins import DestroyModelMixin

from apps.views.main.user import make_login_log


class AddAddressListView(GenericViewSet, APIListModelMixin):
    queryset = Address.objects.all()
    serializer_class = AddressSerializer


class AddressCreateView(GenericViewSet, APICreateModelMixin):
    queryset = Address.objects.all()
    serializer_class = AddressSerializer

class AddressUpdateView(GenericViewSet):
    def create(self, request):
        try:
            id = request.GET.get('ids')
            addresses = Address.objects.get(id=id)
        except Address.DoesNotExist:
            make_login_log(request)
            return APIResponse(code=1, msg='对象不存在')
        Address.objects.filter(id=id).update(default=False)
        serializer = AddressSerializer(addresses, data=request.data)
        if serializer.is_valid():
            serializer.save()
        return APIResponse(code=0, msg='更新成功', data=serializer.data)
class AddressDeleteView(GenericViewSet):
    def create(self, request):
        try:
            ids = request.GET.get('ids')
            Address.objects.filter(id=ids).delete()
        except Address.DoesNotExist:
            make_login_log(request)
            return APIResponse(code=1, msg='对象不存在')
        return APIResponse(code=0, msg='删除成功')
